package cn.com.pansky.action.userList;

import java.util.ArrayList;
import java.util.List;

import net.vsame.url2sql.action.CommonAction;
import net.vsame.url2sql.helper.SqlHelper;
import net.vsame.url2sql.sql.Model;
import net.vsame.url2sql.utils.PostParam;
import cn.com.pansky.helper.PostHelper;
import cn.com.pansky.sdk.API;
import cn.com.pansky.util.ProtectUtil;

import com.alibaba.fastjson.JSONObject;

public class FriendListAction extends CommonAction {

	public void friendList() {
		String findAllInfo = urlConfig.getVar("findAllInfo");
		String aa032 = user.getString("AA032");
		List<Model> list = SqlHelper.query(findAllInfo, aa032, aa032);
		List<Model> list2 = new ArrayList<Model>();
		for (int i = 0; i < list.size(); i++) {
			Model model = check_user(list.get(i), aa032);
			list2.add(model);
		}
		for (int i = 0; i < list2.size(); i++) {
			Model model = list2.get(i);
			String CA006 = ProtectUtil.help1(model.getString("CA006"));
			model.remove("CA006");
			model.add("CA006", CA006);
		}
		context.put("result", list2);
	}

	/**
	 * 检查用户是否参保
	 * 
	 * @param model
	 * @param AA032
	 * @return
	 */
	public Model check_user(Model model, String AA032) {
		String CA003 = model.getString("CA003");
		String CA006 = model.getString("CA006");
		String CA051 = model.getString("CA051");
		String CA050 = model.getString("CA050");
		String sql2 = "select AAA103 from AA11 where AAA100 = 'AAC005' and AAA102 = ? and AAA111 = '1'";
		Model m3 = SqlHelper.queryOne(sql2, CA051);
		CA051 = m3.getString("AAA103");
		model.remove("CA051");
		model.add("CA051", CA051);

		CA050 = CA050.equals("1") ? "男" : "女";
		model.remove("CA050");
		model.add("CA050", CA050);

		String findUser = urlConfig.getVar("findUser");
		Model m = SqlHelper.queryOne(findUser, AA032);
		if (m.getString("CA006").equals(CA006)) {
			model.add("user", 0);
		} else {
			model.add("user", 1);
		}

		JSONObject param = new JSONObject();
		param.put("AAA121", "QA20001");// 业务类型是查询参保
		param.put("AAC002", CA006);
		param.put("AAC003", CA003);
		// 调用业务接口，查询是否已经参保
		JSONObject result = API.sendController("isInsuredController.json",
				param);
		Boolean success = result.getBoolean("SUCCESS");
		if (success) {

			// 已参保组装是否领取待遇，是否已认证等信息
			isRetired(model, CA003, CA006);
			// 组装信息结束
			model.add("insured", 0);// 已参保
			// 如果是已参保的，去业务库拿信息
			getBase(model, AA032);
		} else {
			model.add("insured", 1);// 未参保
			// 如果是未参保，在本地库拿信息
			if (model.getString("user").equals("0")) {// 是当前登陆用户
				String sql = urlConfig.getVar("queryRC10");
				Model m1 = SqlHelper.queryOne(sql, AA032);
				model.add(
						"AAE005",
						m1.getString("AA036") == null ? "" : m1
								.getString("AA036"));
				model.add("AAE006", "");
			} else {// 当前用户的好友
				String sql = urlConfig.getVar("queryRA12");
				Model m1 = SqlHelper.queryOne(sql, AA032,
						model.getString("CA001"));
				model.add("AAE005", m1.getString("CA008"));
				model.add("AAE006", "");
			}
		}
		return model;
	}

	/**
	 * 业务系统拿基本信息
	 * @param model
	 * @param AA032
	 */
	private void getBase(Model model, String AA032) {
		JSONObject params = new JSONObject();
		params.put("AAC002", model.getString("CA006"));
		params.put("AAC003", model.getString("CA003"));
		params.put("AAA121", "QA20001");
		JSONObject json = API.sendController("insuredQueryController.json",
				params);
		String AAE006 = json.getJSONObject("MSG").getString("AAE006");
		String AAE005 = json.getJSONObject("MSG").getString("AAE005");
		String AAB301 = json.getJSONObject("MSG").getString("AAB301");
		if (AAE005.equals("") || AAE005 == null || AAE005.equals(" ")) {
			if (model.getString("user").equals("0")) {// 是当前登陆用户
				String sql = urlConfig.getVar("queryRC10");
				Model m1 = SqlHelper.queryOne(sql, AA032);
				model.add("AAE005", m1.getString("AA036"));
			} else {// 当前用户的好友
				String sql = urlConfig.getVar("queryRA12");
				Model m1 = SqlHelper.queryOne(sql, AA032,
						model.getString("CA001"));
				model.add("AAE005", m1.getString("CA008"));
			}
		} else {
			model.add("AAE005", AAE005);
		}
		model.add("AAB301", AAB301);
		model.add("AAE006", AAE006);
	}

	/**
	 * 获取人员是否是领取待遇人员，是否认证通过
	 * @param model
	 * @param CA003
	 * @param CA006
	 */
	private void isRetired(Model model, String CA003, String CA006) {
		List<PostParam> p = new ArrayList<PostParam>();
		PostParam AAC002 = new PostParam("AAC002", "string", CA006);// 身份证号
		PostParam AAC003 = new PostParam("AAC003", "string", CA003);// 姓名
		p.add(AAC002);
		p.add(AAC003);
		JSONObject results = PostHelper.send("auth/isNeedAuth.json", p);
		boolean isRetired = (Boolean) results.get("isRetired");
		boolean isAuthed = (Boolean) results.get("isAuthed");

		model.add("isRetired", isRetired ? 0 : 1);
		model.add("isAuthed", isAuthed ? 0 : 1);
	}

}
